cr_mount_clear_market_variables_effect = {
	set_variable = { which = cr_mount_cost_5 value = 0 }
	set_variable = { which = cr_mount_cost_10 value = 0 }
	set_variable = { which = cr_mount_cost_15 value = 0 }
	set_variable = { which = cr_mount_cost_25 value = 0 }
}

cr_mount_calculate_market_costs_effect = {
	cr_mount_clear_market_variables_effect = yes
	export_to_variable = { which = cr_mount_cost_5 value = wealth }
	export_to_variable = { which = cr_mount_cost_10 value = wealth }
	export_to_variable = { which = cr_mount_cost_15 value = wealth }
	export_to_variable = { which = cr_mount_cost_25 value = wealth }
	multiply_variable = { which = cr_mount_cost_5 value = -0.05 }
	multiply_variable = { which = cr_mount_cost_10 value = -0.10 }
	multiply_variable = { which = cr_mount_cost_15 value = -0.15 }
	multiply_variable = { which = cr_mount_cost_25 value = -0.25 }
}

cr_mount_remember_new_artifact_effect = {
	new_artifact = {
		save_event_target_as = cr_mount_new_artifact
	}
}

cr_mount_grant_tier_1_effect = {
	random_list = {
		60 = {
			add_artifact = cr_mount_rouncey
			cr_mount_remember_new_artifact_effect = yes
		}
		40 = {
			add_artifact = cr_mount_sumpter_horse
			cr_mount_remember_new_artifact_effect = yes
		}
	}
}

cr_mount_grant_tier_2_effect = {
	random_list = {
		20 = {
			add_artifact = cr_mount_palfrey
			cr_mount_remember_new_artifact_effect = yes
		}
		100 = {
			trigger = { capital_scope = { region = world_europe_west_brittania } }
			add_artifact = cr_mount_irish_hobby
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_europe_north } }
			add_artifact = cr_mount_icelandic_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_europe_north } }
			add_artifact = cr_mount_fjord_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		60 = {
			trigger = { capital_scope = { region = world_europe_west_iberia } }
			add_artifact = cr_mount_iberian_jennet
			cr_mount_remember_new_artifact_effect = yes
		}
		40 = {
			trigger = { capital_scope = { region = world_europe_west_iberia } }
			add_artifact = cr_mount_galician_garrano
			cr_mount_remember_new_artifact_effect = yes
		}
		100 = {
			trigger = { capital_scope = { region = world_himalayas } }
			add_artifact = cr_mount_tibetan_pony
			cr_mount_remember_new_artifact_effect = yes
		}
		100 = {
			trigger = { capital_scope = { region = world_india } }
			add_artifact = cr_mount_manipuri_pony
			cr_mount_remember_new_artifact_effect = yes
		}
	}
}

cr_mount_grant_tier_3_effect = {
	random_list = {
		20 = {
			add_artifact = cr_mount_courser
			cr_mount_remember_new_artifact_effect = yes
		}
		100 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_europe_west_francia
						region = world_europe_west_germania
					}
				}
			}
			add_artifact = cr_mount_frisian_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		60 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_middle_east
						region = world_africa_north
					}
				}
			}
			add_artifact = cr_mount_arabian
			cr_mount_remember_new_artifact_effect = yes
		}
		40 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_europe_west_iberia
						region = world_africa_north
					}
				}
			}
			add_artifact = cr_mount_barb
			cr_mount_remember_new_artifact_effect = yes
		}
		35 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_steppe
						region = world_persia
					}
				}
			}
			add_artifact = cr_mount_turkoman
			cr_mount_remember_new_artifact_effect = yes
		}
		35 = {
			trigger = { capital_scope = { region = world_persia } }
			add_artifact = cr_mount_persian_caspian
			cr_mount_remember_new_artifact_effect = yes
		}
		35 = {
			trigger = { capital_scope = { region = world_steppe } }
			add_artifact = cr_mount_cuman_steppe_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		35 = {
			trigger = { capital_scope = { region = world_steppe_east } }
			add_artifact = cr_mount_jurchen_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_india } }
			add_artifact = cr_mount_marwari
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_india } }
			add_artifact = cr_mount_kathiawari
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_africa_east } }
			add_artifact = cr_mount_dongola
			cr_mount_remember_new_artifact_effect = yes
		}
		50 = {
			trigger = { capital_scope = { region = world_africa_east } }
			add_artifact = cr_mount_abyssinian
			cr_mount_remember_new_artifact_effect = yes
		}
	}
}

cr_mount_grant_tier_4_effect = {
	random_list = {
		15 = {
			add_artifact = cr_mount_charger
			cr_mount_remember_new_artifact_effect = yes
		}
		15 = {
			add_artifact = cr_mount_destrier
			cr_mount_remember_new_artifact_effect = yes
		}
		100 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_europe_west_francia
						region = world_europe_west_germania
					}
				}
			}
			add_artifact = cr_mount_norman_great_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		70 = {
			trigger = { capital_scope = { region = world_steppe } }
			add_artifact = cr_mount_mongol_horse
			cr_mount_remember_new_artifact_effect = yes
		}
		70 = {
			trigger = {
				capital_scope = {
					OR = {
						region = world_persia
						region = world_steppe_east
					}
				}
			}
			add_artifact = cr_mount_ferghana
			cr_mount_remember_new_artifact_effect = yes
		}
	}
}

cr_mount_commander_pay_2_5_effect = {
	export_to_variable = { which = cr_mount_commander_purchase_cost value = wealth }
	multiply_variable = { which = cr_mount_commander_purchase_cost value = -0.025 }
	wealth = cr_mount_commander_purchase_cost
	set_variable = { which = cr_mount_commander_purchase_cost value = 0 }
}

cr_mount_commander_pay_5_effect = {
	export_to_variable = { which = cr_mount_commander_purchase_cost value = wealth }
	multiply_variable = { which = cr_mount_commander_purchase_cost value = -0.05 }
	wealth = cr_mount_commander_purchase_cost
	set_variable = { which = cr_mount_commander_purchase_cost value = 0 }
}

cr_mount_commander_pay_10_effect = {
	export_to_variable = { which = cr_mount_commander_purchase_cost value = wealth }
	multiply_variable = { which = cr_mount_commander_purchase_cost value = -0.10 }
	wealth = cr_mount_commander_purchase_cost
	set_variable = { which = cr_mount_commander_purchase_cost value = 0 }
}

cr_mount_commander_pay_15_effect = {
	export_to_variable = { which = cr_mount_commander_purchase_cost value = wealth }
	multiply_variable = { which = cr_mount_commander_purchase_cost value = -0.15 }
	wealth = cr_mount_commander_purchase_cost
	set_variable = { which = cr_mount_commander_purchase_cost value = 0 }
}

cr_mount_commander_pay_20_effect = {
	export_to_variable = { which = cr_mount_commander_purchase_cost value = wealth }
	multiply_variable = { which = cr_mount_commander_purchase_cost value = -0.20 }
	wealth = cr_mount_commander_purchase_cost
	set_variable = { which = cr_mount_commander_purchase_cost value = 0 }
}

cr_mount_commander_buy_mount_effect = {
	if = {
		limit = { cr_mount_commander_tier_4_access_trigger = yes }
		cr_mount_commander_pay_20_effect = yes
		cr_mount_grant_tier_4_effect = yes
	}
	else_if = {
		limit = { cr_mount_commander_tier_3_access_trigger = yes }
		cr_mount_commander_pay_15_effect = yes
		cr_mount_grant_tier_3_effect = yes
	}
	else_if = {
		limit = { cr_mount_commander_tier_2_access_trigger = yes }
		cr_mount_commander_pay_10_effect = yes
		cr_mount_grant_tier_2_effect = yes
	}
	else = {
		cr_mount_commander_pay_5_effect = yes
		cr_mount_grant_tier_1_effect = yes
	}
}

cr_mount_commander_buy_basic_tack_effect = {
	random_list = {
		10 = {
			trigger = { NOT = { any_artifact = { artifact_type = cr_mount_saddle } } }
			cr_mount_commander_pay_2_5_effect = yes
			add_artifact = cr_mount_saddle
		}
		10 = {
			trigger = { NOT = { any_artifact = { artifact_type = cr_mount_bridle } } }
			cr_mount_commander_pay_2_5_effect = yes
			add_artifact = cr_mount_bridle
		}
		10 = {
			trigger = { NOT = { any_artifact = { artifact_type = cr_mount_horseshoes } } }
			cr_mount_commander_pay_2_5_effect = yes
			add_artifact = cr_mount_horseshoes
		}
		10 = {
			trigger = { NOT = { any_artifact = { artifact_type = cr_mount_saddlebags } } }
			cr_mount_commander_pay_2_5_effect = yes
			add_artifact = cr_mount_saddlebags
		}
	}
}

cr_mount_commander_buy_advanced_tack_effect = {
	random_list = {
		10 = {
			trigger = {
				cr_mount_commander_barding_access_trigger = yes
				NOT = { any_artifact = { artifact_type = cr_mount_barding } }
			}
			cr_mount_commander_pay_5_effect = yes
			add_artifact = cr_mount_barding
		}
		10 = {
			trigger = {
				cr_mount_commander_caparison_access_trigger = yes
				NOT = { any_artifact = { artifact_type = cr_mount_caparison } }
			}
			cr_mount_commander_pay_5_effect = yes
			add_artifact = cr_mount_caparison
		}
		10 = {
			trigger = {
				cr_mount_commander_standard_access_trigger = yes
				NOT = { any_artifact = { artifact_type = cr_mount_battle_standard } }
			}
			cr_mount_commander_pay_5_effect = yes
			add_artifact = cr_mount_battle_standard
		}
	}
}

# Future fantasy modules enable this global flag and call the effect. The core
# market deliberately has no route into this pool.
cr_mount_grant_random_fantasy_mount_effect = {
	if = {
		limit = { has_global_flag = cr_fantasy_mounts_enabled }
		random_list = {
			10 = { add_artifact = cr_mount_unicorn cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_pegasus cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_nightmare cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_hippogriff cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_griffin cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_wyvern cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_dire_wolf cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_great_bear cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_giant_elk cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_giant_boar cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_war_lion cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_skeletal_steed cr_mount_remember_new_artifact_effect = yes }
			10 = { add_artifact = cr_mount_arcane_construct cr_mount_remember_new_artifact_effect = yes }
		}
	}
}

cr_mount_reduce_health_effect = {
	if = {
		limit = { has_artifact_flag = cr_mount_health_healthy }
		clr_artifact_flag = cr_mount_health_healthy
		set_artifact_flag = cr_mount_health_injured
	}
	else_if = {
		limit = { has_artifact_flag = cr_mount_health_injured }
		clr_artifact_flag = cr_mount_health_injured
		set_artifact_flag = cr_mount_health_serious
	}
	else_if = {
		limit = { has_artifact_flag = cr_mount_health_serious }
		clr_artifact_flag = cr_mount_health_serious
		set_artifact_flag = cr_mount_health_dying
	}
}

cr_mount_improve_health_effect = {
	if = {
		limit = { has_artifact_flag = cr_mount_health_dying }
		clr_artifact_flag = cr_mount_health_dying
		set_artifact_flag = cr_mount_health_serious
	}
	else_if = {
		limit = { has_artifact_flag = cr_mount_health_serious }
		clr_artifact_flag = cr_mount_health_serious
		set_artifact_flag = cr_mount_health_injured
	}
	else_if = {
		limit = { has_artifact_flag = cr_mount_health_injured }
		clr_artifact_flag = cr_mount_health_injured
		set_artifact_flag = cr_mount_health_healthy
	}
}

cr_mount_normalize_health_effect = {
	if = {
		limit = {
			has_artifact_flag = cr_mount
			NOR = {
				has_artifact_flag = cr_mount_health_healthy
				has_artifact_flag = cr_mount_health_injured
				has_artifact_flag = cr_mount_health_serious
				has_artifact_flag = cr_mount_health_dying
			}
		}
		set_artifact_flag = cr_mount_health_healthy
	}
}
